package org.csu.forum.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.forum.domain.PO.*;
import org.csu.forum.domain.VO.Prior;
import org.csu.forum.mapper.*;
import org.csu.forum.service.AccountManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class AccountManageServiceImpl implements AccountManageService {

    @Autowired
    AccountMapper accountMapper;

    @Autowired
    BasicPriorMapper basicPriorMapper;

    @Autowired
    ManagePriorMapper managePriorMapper;

    @Autowired
    UserpriorMapper userPriorMapper;

    @Autowired
    LoginMapper loginMapper;

    @Override
    public List<Account> getAccountList() {
        return accountMapper.selectList(null);
    }

    @Override
    public void deleteAccount(int userid) {
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq("userid",userid);
        accountMapper.delete(accountQueryWrapper);
    }

    @Override
    public List<Prior> getPriorList(int userid) {
        QueryWrapper<Userprior> userpriorQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Basicprior> basicpriorQueryWrapper = new QueryWrapper<>();
        QueryWrapper<ManagePrior> managePriorQueryWrapper = new QueryWrapper<>();
        List<Prior>priorList = new ArrayList<Prior>();
        userpriorQueryWrapper.eq("userid",userid);
        List<Userprior> userpriorList = userPriorMapper.selectList(userpriorQueryWrapper);
        Iterator<Userprior> userpriorIterator = userpriorList.iterator();
        while(userpriorIterator.hasNext()){
            Userprior userprior = userpriorIterator.next();
            managePriorQueryWrapper.clear();
            basicpriorQueryWrapper.clear();
            basicpriorQueryWrapper.eq("basic_pid",userprior.getBasicpid());
            managePriorQueryWrapper.eq("manage_pid",userprior.getManagepid());

            Basicprior basicprior = basicPriorMapper.selectOne(basicpriorQueryWrapper);
            ManagePrior managePrior = managePriorMapper.selectOne(managePriorQueryWrapper);
            Prior prior = new Prior();

            prior.setUserid(userid);
            prior.setManage_rw_prior(managePrior.getManageRwPrior());
            prior.setSet_top_prior(managePrior.getSetTopPrior());
            prior.setSectionid(userprior.getSectionid());
            prior.setSet_essence_prior(managePrior.getSetEssencePrior());
            if(basicprior.getBanReadDeadline() != null)
                prior.setBan_read_deadline(basicprior.getBanReadDeadline());
            if(basicprior.getBanWriteDeadline() != null)
                prior.setBan_write_deadline(basicprior.getBanWriteDeadline());
            priorList.add(prior);
        }

        return priorList;
    }

    @Override
    public void setPrior(Prior prior) {
        QueryWrapper<Userprior>userpriorQueryWrapper = new QueryWrapper<>();
        int sectionId = prior.getSectionid();
        userpriorQueryWrapper.eq("sectionid",sectionId).eq("userid",prior.getUserid());

        Userprior userprior = userPriorMapper.selectOne(userpriorQueryWrapper);

        if(userprior == null)
            return;

        UpdateWrapper<Basicprior>basicpriorUpdateWrapper = new UpdateWrapper<>();
        UpdateWrapper<ManagePrior>managePriorUpdateWrapper = new UpdateWrapper<>();

        basicpriorUpdateWrapper.eq("basic_pid",userprior.getBasicpid());
        managePriorUpdateWrapper.eq("manage_pid",userprior.getManagepid());

        Basicprior basicprior = new Basicprior();
        basicprior.setBasicPid(userprior.getBasicpid());
        basicprior.setBanReadDeadline(prior.getBan_read_deadline());
        basicprior.setBanWriteDeadline(prior.getBan_write_deadline());

        ManagePrior managePrior = new ManagePrior();
        managePrior.setManageRwPrior(prior.getManage_rw_prior());
        managePrior.setManagePid(userprior.getManagepid());
        managePrior.setSetEssencePrior(prior.getSet_essence_prior());
        managePrior.setSetTopPrior(prior.getSet_top_prior());


        basicPriorMapper.update(basicprior,basicpriorUpdateWrapper);
        managePriorMapper.update(managePrior,managePriorUpdateWrapper);

    }

    @Override
    public void addAccountToSection(int userid, int sectionid) {

        int basic_pid = userid * 10000 + sectionid;
        int manage_pid = userid * 10000 + sectionid;

        Basicprior basicprior = new Basicprior();
        basicprior.setBasicPid(basic_pid);


        ManagePrior managePrior = new ManagePrior();
        managePrior.setManagePid(manage_pid);
        managePrior.setManageRwPrior(false);
        managePrior.setSetTopPrior(false);
        managePrior.setSetEssencePrior(false);

        Userprior userprior = new Userprior();

        userprior.setUserid(userid);
        userprior.setSectionid(sectionid);
        userprior.setBasicpid(basic_pid);
        userprior.setManagepid(manage_pid);

        basicPriorMapper.insert(basicprior);
        managePriorMapper.insert(managePrior);
        userPriorMapper.insert(userprior);
    }

    @Override
    public void insertAccount(Account account ,Login login){
        accountMapper.insert(account);
        loginMapper.insert(login);
    }
}
